package love.simbot.example.listen;


import catcode.CatCodeUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;;
import jdk.nashorn.internal.ir.CallNode;
import love.forte.common.ioc.annotation.Beans;
import love.forte.common.ioc.annotation.Depend;
import love.forte.simbot.annotation.*;
import love.forte.simbot.api.message.MessageContent;
import love.forte.simbot.api.message.MessageContentBuilderFactory;
import love.forte.simbot.api.message.events.GroupMsg;
import love.forte.simbot.api.message.events.MessageGet;
import love.forte.simbot.api.message.events.PrivateMsg;
import love.forte.simbot.api.sender.Getter;
import love.forte.simbot.api.sender.Sender;
import love.forte.simbot.constant.PriorityConstant;
import love.simbot.example.constant.Constant;
import love.simbot.example.entity.baidu.TranslationMapper;
import love.simbot.example.entity.baidu.TranslationResult;
import love.simbot.example.entity.chatRobot.ChatRobot;
import love.simbot.example.entity.chatRobot.MoliChatRobot;
import love.simbot.example.entity.epidemic.LocationMessage;
import love.simbot.example.entity.wangyi.newSong.QQSongResult;
import love.simbot.example.entity.wangyi.newSong.WangYiSongResult;
import love.simbot.example.entity.wangyi.search.QQResult;
import love.simbot.example.entity.wangyi.search.SearchResult;
import love.simbot.example.entity.wangyi.songDetail.SongResult;
import love.simbot.example.listen.listenServer.GroupService;
import love.simbot.example.listen.listenServer.ListenerService;
import love.simbot.example.utils.baidu.TransApi;
import love.simbot.example.utils.chatRobot.ChatRobotUtils;
import love.simbot.example.utils.chatRobot.RobotUtils;
import love.simbot.example.utils.epidemic.EpidemicHttpUtils;
import love.simbot.example.utils.wangyi.MusicUtils;

import java.awt.*;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.*;


// Beans:表示交给Simbot来监管
// Linstener:类似于Beans,用于事件监听
@Beans
public class ProvideListener {





    // 获取一个消息构建工厂
    @Depend
    private MessageContentBuilderFactory messageContentBuilderFactory;




    // OnPrivate表示是私聊
    // privateMsg表示获取私聊消息对象
    // Sender是一个消息发送器
    @OnPrivate
    @Priority(value = PriorityConstant.FIRST)
    public void replayAimerMaster(PrivateMsg privateMsg,
                                  MessageGet getter,
                                  Sender sender,
                                  Getter get){

        MessageContent msgContent = privateMsg.getMsgContent();
        System.out.println("使用者:"+getter.getAccountInfo().getAccountCodeNumber()+"\t"+"输入的内容:"+msgContent.getMsg());

        // 获取菜单处理
        String message = msgContent.getMsg();
        long account = getter.getAccountInfo().getAccountCodeNumber();
        String code = ListenerService.codeMap.get(account);
        // 是否是聊天模式,以及是否关闭
        if(code != null && !("exit".equals(message)) && "D".equals(code)){
            ListenerService.chatMessageGet(sender,getter);
        }
        else if(code != null && "exit".equals(message) && "D".equals(code)){
            ListenerService.codeMap.remove(account);
            sender.sendPrivateMsg(account,"已退出聊天模式");
        }

        // 上次的指令对下次有影响的
        if(code != null){
            nextMethod(sender,code,getter);
            return;
        }

        if(GroupService.groupRepresent.containsKey(getter.getAccountInfo().getAccountCode()) && !message.equals("getOut")){
            sender.sendGroupMsg(GroupService.groupRepresent.get(getter.getAccountInfo().getAccountCode()),message);
        }
        else if(GroupService.groupRepresent.containsKey(getter.getAccountInfo().getAccountCode()) && message.equals("getOut")) {
            sender.sendPrivateMsg(getter,"代理模式关闭成功");
            GroupService.groupRepresent.remove(getter.getAccountInfo().getAccountCode());
        }


        // 有定时任务需要取消的
        if(ListenerService.timeTaskMap.get(account) != null && "cancel".equals(message)){
            ListenerService.cancelTimeTask(account,sender);
        }


        if(message.equals("菜单")){
            // 1.获取私有菜单
            getProvideMenu(privateMsg,sender);
        }
        // 执行菜单的中的某些方法
        if(message.matches("^[\\w] [\\s\\S]*")){
            invokeMenuMethod(message,privateMsg,getter, get, sender);
        }
        return;
    }



    // 执行菜单的方法
    private void invokeMenuMethod(String myMessage,
                                  PrivateMsg privateMsg,
                                  MessageGet getter,
                                  Getter get,
                                  Sender sender) {

        //1.将用户输入的指令进行拆解
        char instruction = myMessage.charAt(0);
        switch (instruction){
            case 'A':
                sender.sendPrivateMsg(privateMsg,Constant.TIP);
                break;
            case 'B':
                ListenerService.searchMusic(myMessage,sender,getter);
                // 歌曲搜索
                break;
            case 'C':
                // 语言翻译
                ListenerService.translationLanguage(myMessage,privateMsg,sender);
                break;
            case 'D':
                ListenerService.chatModeStart(sender,getter);
                break;
            case 'E':
                // 定时提醒
                ListenerService.regularlyRemind(getter,sender);
                break;
            case 'G':
                // 疫情查询
                ListenerService.epidmicGet(sender,getter);
                break;
            case 'H':
                // B站视频搜索
                ListenerService.searchBiliBiliVideo(sender,getter);
                break;
            case 'I':
                // 代言模式
                ListenerService.startRepersentSepeakMode(getter,sender,get);
                break;
            case 'J':
                ListenerService.sendImage(getter,sender);
                break;
        }

    }

    // 获取私聊菜单
    private   void getProvideMenu(PrivateMsg privateMsg,Sender sender){
        sender.sendPrivateMsg(privateMsg, Constant.PROVIDER_MENU);
    }



    private void nextMethod(Sender sender,String code,MessageGet messageGet){
        char index = code.charAt(0);
        switch (index){
            case 'B':
                ListenerService.shareMusic(sender,messageGet);
                break;
            case 'H':
                ListenerService.shareBiliBiliVideo(sender,messageGet);
                break;
            case 'I':
               ListenerService.repersentSpeak(sender,messageGet);
                break;
        }
    }



}
